#!/bin/bash

set -e # exit on error
set -x # echo commands

if  [[ ! -z "${TRAVIS_TAG}" ]]; then
    echo
    echo "SKIPPING TEST STAGE INSTALL --- This is a build for release tag ${TRAVIS_TAG}"
    echo
    exit 0
fi

# if this is an external contributor then build assets were not uploaded
if [[ -z "${SAUCE_USERNAME}" ]]; then
    echo
    echo "RE-RUNNING BUILD STEPS --- external committer"
    echo
    bash "scripts/ci/install.build"
    bash "scripts/ci/build"
else
    # do the base install
    bash scripts/ci/install

    pushd ${HOME}

    # download the pre-built Bokeh package from the build phase
    wget -nv "https://ci.bokeh.org/travis/${TRAVIS_BUILD_ID}/build/conda-bld-noarch.tgz"
    tar xzf conda-bld-noarch.tgz

    # download the previous BokehJS build directory to run tests
    wget -nv "https://ci.bokeh.org/travis/${TRAVIS_BUILD_ID}/build/bokehjs-build.tgz"
    tar xzf bokehjs-build.tgz

    popd
fi

# install the pre-built Bokeh package plus all test and runtime dependencies
conda install -n testenv --yes --quiet --use-local bokeh `python scripts/deps.py run test` tornado=${TORNADO:-5}

npm install -g npm

# install Bokeh sample data needed test collector
python -c 'import bokeh; bokeh.sampledata.download(progress=False)'

# execute any test-specific install scripts
if [[ -f "scripts/ci/install.${GROUP}" ]]; then
    bash "scripts/ci/install.${GROUP}"
fi

# regrettably need to have previous JS build in the source tree, and it also
# may require npm files installed in the test-specific scripts
python setup.py --install-js
